package com.bindingfly.client.formrender;

import com.bindingfly.client.formrender.factory.WidgetFactor;
import com.bindingfly.client.formrender.widget.impl.SimplePanelFly;
import com.bindingfly.client.formrender.widget.impl.SubmitButtonFly;
import com.bindingfly.client.formrender.widget.impl.TextAreaFly;
import com.bindingfly.client.formrender.widget.impl.TextBoxFly;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class DynamicRendererWidget extends VerticalPanel
{
	RequestRendererWidget requestRendererWidget;
	TextArea editTextArea;	
	
	public DynamicRendererWidget()
	{
		setSpacing(5);
		
		add(new HTML("<a href=\"http://www.youtube.com/watch?v=wDzuS7pnYJE\">YouTube video on how to use this page</a>"));
		
		editTextArea = new TextArea();

			
		editTextArea.setCharacterWidth(50);
		editTextArea.setVisibleLines(10);

		editTextArea.setValue("TextBox <span id=\"text1\" widgetType=\"textBox\"></span><br>TextArea <span id=\"text2\" widgetType=\"textArea\"></span><br>SubmitButton <span id=\"submit1\" widgetType=\"submitButton\"></span>");
		

		add(editTextArea);

		requestRendererWidget = new RequestRendererWidget("dynamicRootElement");
		
		Button updateFormButton = new Button();
		updateFormButton.setText("Bind on the fly");
		
		updateFormButton.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event)
			{
				// TODO Auto-generated method stub
				requestRendererWidget.defineRequestDefinition(editTextArea.getValue());
			}});
		
		
		add(updateFormButton);
		
		
		add(requestRendererWidget);	
		
		initWidgetFactory();
		
	}
	
	final private void initWidgetFactory()
	{
		WidgetFactor.injectableWidgets.put("textArea", new TextAreaFly());
		WidgetFactor.injectableWidgets.put("textBox", new TextBoxFly());
		WidgetFactor.injectableWidgets.put("submitButton", new SubmitButtonFly());
		
		WidgetFactor.injectableContainers.put("panel", new SimplePanelFly());
	}

	public String getTemplateDefinition()
	{
		return editTextArea.getValue();
	}
	



	


}
